<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-card>
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="searchForm"
        label-width="80px"
      >
        <el-form-item label="讲师名称" prop="name">
          <el-input v-model="searchForm.name" />
        </el-form-item>
        <el-form-item label="讲师头衔" prop="level">
          <el-select v-model="searchForm.level" placeholder="请选择头衔">
            <el-option label="高级讲师" :value="1" />
            <el-option label="首席讲师" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="添加时间" prop="begin">
          <el-date-picker
            v-model="searchForm.begin"
            type="datetime"
            placeholder="请选择开始时间"
          />
        </el-form-item>
        <el-form-item prop="end">
          <el-date-picker
            v-model="searchForm.end"
            type="datetime"
            placeholder="请选择结束时间"
          />
        </el-form-item>
        <el-form-item style="text-align:center;width:100%;">
          <el-button
            icon="el-icon-search"
            type="primary"
            @click="search"
          >查询</el-button>
          <el-button type="default" @click="reset">清空</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 内容区域 -->
    <el-card style="margin-top:20px;">
      <el-table :data="teacherList" style="width: 100%">
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column prop="name" label="名称" />
        <el-table-column label="头衔">
          <template slot-scope="scope">
            <span>{{ scope.row.level === 1 ? '高级讲师' : '首席讲师' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="career" label="资历" width="180" />
        <el-table-column prop="gmtCreate" label="添加时间" width="180" />
        <el-table-column prop="sort" label="排序" />
        <el-table-column label="操作" width="220">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="$router.push(`/edu/teacher/edit/${scope.row.id}`)"
            >修改</el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="del(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <div style="text-align:center;height:50px;padding-top:20px;">
      <el-pagination
        :current-page="page"
        :page-sizes="[5, 10, 20, 50, 100]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getTeacherList, deleteTeacher } from '@/api/edu/teacher'
export default {
  data() {
    return {
      searchForm: {
        name: null, // 名称
        level: null, // 头衔 1高级讲师 2首席讲师
        begin: null, // 开始时间
        end: null // 结束时间
      },
      page: 1,
      limit: 5,
      total: 0,
      teacherList: [] // 讲师列表
    }
  },
  created() {
    this.getTeacherListData()
  },
  methods: {
    async getTeacherListData() {
      const res = await getTeacherList(this.page, this.limit, this.searchForm)

      this.teacherList = res.data.items
      this.total = res.data.total
    },
    search() {
      this.page = 1

      this.getTeacherListData()
    },
    reset() {
      this.$refs.searchFormRef.resetFields()
      this.search()
    },
    // 页容量改变
    handleSizeChange(size) {
      this.limit = size

      this.search()
    },
    // 页码改变
    handleCurrentChange(page) {
      this.page = page

      this.getTeacherListData()
    },
    // 删除
    del(id) {
      this.$confirm('确定删除该条记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await deleteTeacher(id)

          this.search()
        })
        .catch(() => {})
    }
  }
}
</script>
